package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.CraftProgram;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.CraftProductProgram;
import com.uwlaser.service.CraftProductProgramService;
import com.uwlaser.service.dto.CraftProductProgramDto;
import com.uwlaser.service.dto.CraftProductProgramQueryParam;
import com.uwlaser.service.mapper.CraftProductProgramMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-26
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = CraftProductProgramService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CraftProductProgramServiceImpl extends CommonServiceImpl<CraftProductProgramMapper, CraftProductProgram> implements CraftProductProgramService {

    // private final RedisUtils redisUtils;
    private final CraftProductProgramMapper craftProductProgramMapper;

    @Override
    public PageInfo<CraftProductProgramDto> queryAll(CraftProductProgramQueryParam query, Pageable pageable) {
        IPage<CraftProductProgram> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<CraftProductProgram> page = craftProductProgramMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, CraftProductProgramDto.class);
    }

    @Override
    public List<CraftProductProgramDto> queryAll(CraftProductProgramQueryParam query){
        return ConvertUtil.convertList(craftProductProgramMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), CraftProductProgramDto.class);
    }

    @Override
    public CraftProductProgram getById(Integer id) {
        return craftProductProgramMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public CraftProductProgramDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), CraftProductProgramDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CraftProductProgramDto resources) {
        CraftProductProgram entity = ConvertUtil.convert(resources, CraftProductProgram.class);
        CraftProductProgram craftProgram = new CraftProductProgram();
        craftProgram.setOperationCode(entity.getOperationCode());
        craftProgram.setProductCode(entity.getProductCode());
        Integer integer = craftProductProgramMapper.selectCount(new QueryWrapper<>(craftProgram));
        if(integer > 0){
            throw new RuntimeException("当前产品【"+craftProgram.getProductCode()+"】【"+craftProgram.getOperationCode()+"】已设置程序号！");
        }
        return craftProductProgramMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(CraftProductProgramDto resources){
        CraftProductProgram entity = ConvertUtil.convert(resources, CraftProductProgram.class);
        int ret = craftProductProgramMapper.updateById(entity);
        CraftProductProgram craftProgram = new CraftProductProgram();
        craftProgram.setOperationCode(entity.getOperationCode());
        craftProgram.setProductCode(entity.getProductCode());
        Integer integer = craftProductProgramMapper.selectCount(new QueryWrapper<>(craftProgram));
        if(integer >= 2){
            throw new RuntimeException("当前产品【"+craftProgram.getProductCode()+"】【"+craftProgram.getOperationCode()+"】已设置程序号！");
        }
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return craftProductProgramMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<CraftProductProgramDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (CraftProductProgramDto craftProductProgram : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品编号", craftProductProgram.getProductCode());
              map.put("工序编号", craftProductProgram.getOperationCode());
              map.put("程序号", craftProductProgram.getProgramOn());
              map.put("描述", craftProductProgram.getRemark());
              map.put("创建人", craftProductProgram.getCreateBy());
              map.put("创建时间", craftProductProgram.getCreateTime());
              map.put("更新人", craftProductProgram.getUpdateBy());
              map.put("更新时间", craftProductProgram.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
